package com.zhuoshi.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhuoshi.bean.SaleQuery;
import com.zhuoshi.bean.WdApartmentLayout;
import com.zhuoshi.bean.WdHtBuild;
import com.zhuoshi.bean.WdHtProject;
import com.zhuoshi.bean.WdHtRoom;
import com.zhuoshi.bean.WdHtUnit;
import com.zhuoshi.bean.WdSaleChance;
import com.zhuoshi.bean.WdUser;
import com.zhuoshi.service.WdHtBuildService;
import com.zhuoshi.service.WdHtCustomerService;
import com.zhuoshi.service.WdHtProjectService;
import com.zhuoshi.service.WdHtUnitService;
import com.zhuoshi.service.WdRoomService;
import com.zhuoshi.service.WdSaleChanceService;
import com.zhuoshi.service.WdUserService;
import com.zhuoshi.serviceImpl.WdRoomServiceImpl;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/salechance")
public class WdSaleChanceController {

	@Autowired
	private WdRoomService WdRoomServiceImpl;

	@Autowired
	private WdSaleChanceService wsservice;
	@Autowired
	private WdUserService wumservice;
	@Autowired
	private WdHtProjectService whproject;
	@Autowired
	private WdHtBuildService whbuild;
	@Autowired
	private WdHtUnitService whunit;
	@Autowired
	private WdRoomService whroom;
	@Autowired
	private WdHtCustomerService whcustomer;

	// 项目的service
	@Autowired
	private WdHtProjectService wdProjectService;
	@Autowired
	private WdSaleChanceService service;

	/**
	 * 查询全部，模糊查询
	 * 
	 * @param bean
	 * @param model
	 * @return
	 */
	@RequestMapping("/WdSaleChanceList.do")
	public String list(SaleQuery bean, Model model) {
		List<WdSaleChance> SaleChanceList = service.getlist(bean);
		model.addAttribute("SaleChanceList", SaleChanceList);
		return "WdSaleChanceList";

	}

	/**
	 * 删除
	 * 
	 * @param saleChanceId
	 * @return
	 */
	@RequestMapping("/WdSaleChanceDelete.do")
	public String delete(String saleChanceId) {
		service.deleteByPrimaryKey(saleChanceId);
		return "redirect:WdSaleChanceList.do";
	}

	/**
	 * 添加页面跳转
	 * 
	 * @return
	 */
	@RequestMapping("/WdSaleChanceToAdd.do")
	public String toadd(HttpServletRequest ht,Model model) {
		//调用service 获取所有的项目  保存到域
		List <WdHtProject> list = service.getAllProject();
		ht.setAttribute("ddddd", list);
        List <WdHtRoom> list1 = service.selectAll();		
        ht.setAttribute("aaaa", list1);
//		List<WdHtRoom> list1 = whroom.selectAll(null);
//		model.addAttribute("roomList", list1);
		return "WdSaleChanceAdd";
	}

	/**
	 * 动态添加
	 * 
	 * @param chance
	 * @return
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("/WdSaleChanceAdd.do")
	public String insert(WdSaleChance wsmodel, HttpServletRequest req, String projectId, String buildId, String unitId,
			String roomId, String saleChanceName, String intentionDescription, String customerName, String tel,
			String userName) throws Exception {
		String replace = UUID.randomUUID().toString().replace("-", "");
		wsmodel.setSaleChanceId(replace);
		wsmodel.setProjectId(projectId);
		wsmodel.setBuildId(buildId);
		wsmodel.setUnitId(unitId);
		wsmodel.setRoomId(roomId);
		wsmodel.setSaleChanceName(saleChanceName);
		wsmodel.setIntentionDescription(intentionDescription);
		wsmodel.setCustomerName(customerName);
		wsmodel.setTel(tel);
		wsmodel.setUserName(userName);
		wsmodel.setState("草稿");
		wsmodel.setAddtime(new SimpleDateFormat("yyyy-MM-dd").parse(new Date().toLocaleString().substring(0, 8)));
		//wsmodel.setNumber(((WdUser) req.getSession().getAttribute("user")).getNumber());
		wsmodel.setDeleteState("0");
		service.insertSelective(wsmodel);
		return "redirect:WdSaleChanceList.do";
	}
	// 查询项目ajax
	@RequestMapping("selectProjectAll")
	public @ResponseBody Map<String, Object> selectProject() {
		System.out.println("进入项目");
		WdHtProject record = new WdHtProject();
		List<WdHtProject> selectAll = whproject.getAll(record);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		List list = null;
		if (null != selectAll && selectAll.size() > 0) {
			list = new ArrayList();
			for (WdHtProject bean : selectAll) {
				System.out.println(bean.getProjectName() + "项目");
				Map<String, String> taskMap = new HashMap<String, String>();
				taskMap.put("projectId", bean.getProjectId());
				taskMap.put("projectName", bean.getProjectName());
				list.add(taskMap);
			}
		}
		jsonMap.put("tasks", list);
		return jsonMap;
	}

	// 查询单元ajax
	@RequestMapping("selectBuild")
	public @ResponseBody Map<String, Object> selectBuild(String projectId) {
		List<WdHtBuild> selectAll = whbuild.getAll();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		List list = null;
		if (null != selectAll && selectAll.size() > 0) {
			list = new ArrayList();
			for (WdHtBuild bean : selectAll) {
				Map<String, String> taskMap = new HashMap<String, String>();
				taskMap.put("buildId", bean.getBuildId());
				taskMap.put("buildName", bean.getBuildName());
				list.add(taskMap);
			}
		}
		jsonMap.put("tasks", list);
		return jsonMap;
	}

	// selectByBuildId(buildId);
	// 查询单元ajax
	@RequestMapping("selectUnit")
	public @ResponseBody Map<String, Object> selectUnit(String buildId) {
		WdHtProject record = new WdHtProject();
		List<WdHtUnit> selectAll = whunit.getAll(buildId);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		List list = null;
		if (null != selectAll && selectAll.size() > 0) {
			list = new ArrayList();
			for (WdHtUnit bean : selectAll) {
				Map<String, String> taskMap = new HashMap<String, String>();
				taskMap.put("unitId", bean.getUnitId());
				taskMap.put("unitName", bean.getUnitCode());
				list.add(taskMap);
			}
		}
		jsonMap.put("tasks", list);
		return jsonMap;
	}

	// 查询房间ajax
	@RequestMapping("room")
	public void selectRoom(String unitId,HttpServletResponse hsr) throws IOException {
		//System.out.println("进入room");
		WdHtRoom record = new WdHtRoom();
		record.setUnitId(unitId);
		List<WdHtRoom> selectAll = whroom.selectAllRoom(record);
		//System.out.println(selectAll.size() + "room");
		JSONArray fromObject = JSONArray.fromObject(selectAll);
		hsr.getWriter().print(fromObject.toString());
	}

}
